﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PresentationActioner.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The presentation actioner.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Actioners.Activities
{
    using System;

    using NHibernate;

    using ServiceLibrary.Models.Base;
    using ServiceLibrary.Models.Domain;
    using ServiceLibrary.Services.Commands;
    using ServiceLibrary.Services.Queries;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The presentation actioner.
    /// </summary>
    public class PresentationActioner : ActionerBase
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PresentationActioner"/> class.
        /// </summary>
        /// <param name="session">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public PresentationActioner(ISession session, User activeUser)
            : base(session, activeUser)
        {
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The create presentation.
        /// </summary>
        /// <param name="presentation">
        /// The presentation.
        /// </param>
        /// <returns>
        /// The <see cref="Guid"/>.
        /// </returns>
        public Guid CreatePresentation(Presentation presentation)
        {
            presentation.CreatedDateTime = DateTime.Now;
            this.Session.Save(presentation);

            this.Log.CreateLogEntry(
                presentation.ID, 
                string.Format("Presentation '{0}', blev oprettet.", presentation.Name), 
                Enums.LogEntryType.Presentation);

            return presentation.ID;
        }

        /// <summary>
        /// The create presentation.
        /// </summary>
        /// <param name="ctc">
        /// The ctc.
        /// </param>
        public void CreatePresentation(CreatePresentationCommand ctc)
        {
            var pres = new Presentation
                           {
                               ID = ctc.Dto.ID, 
                               Name = ctc.Dto.Name
                           };

            CreatePresentation(pres);
        }

        /// <summary>
        /// The execute search query.
        /// </summary>
        /// <param name="query">
        /// The query.
        /// </param>
        /// <returns>
        /// The <see cref="PresentationResult"/>.
        /// </returns>
        public PresentationResult ExecuteSearchQuery(PresentationQuery query)
        {
            SearchSettings settings = query.Settings;

            var result = new PresentationResult();

            IQueryOver<Presentation, Presentation> search = this.Session.QueryOver<Presentation>();
            this.ApplyDefaultCriterias(settings, search);

            result.TotalRowCount = search.RowCount();

            var presentations = ApplySkipAndTake(settings, search);

            foreach (Presentation presentation in presentations)
            {
                var dto = Services.Dto.Presentation.Convert(presentation);
                if (dto != null)
                {
                    result.List.Add(dto);
                }
            }

            return result;
        }

        /// <summary>
        /// The get user.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// The <see cref="Presentation"/>.
        /// </returns>
        public Presentation GetUser(Guid id)
        {
            var presentation = this.Session.Get<Presentation>(id);

            return presentation;
        }

        #endregion

        #region Methods

        /// <summary>
        /// The delete presentation.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        internal void DeletePresentation(DeletePresentationCommand cmd)
        {
            var pres = this.Session.Get<Presentation>(cmd.Dto);

            if (pres != null)
            {
                int partnerUsage =
                    this.Session.QueryOver<NetworkingActivity>().Where(x => x.Presentations.Contains(pres)).RowCount();
                if (partnerUsage == 0)
                {
                    this.Session.Delete(pres);
                }
            }
        }

        /// <summary>
        /// The update presentation.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        internal void UpdatePresentation(UpdatePresentationCommand cmd)
        {
            if (cmd.Dto != null)
            {
                var pres = this.Session.Get<Presentation>(cmd.Dto.ID);
                if (pres != null)
                {
                    pres.Name = cmd.Dto.Name;
                    this.Session.Update(pres);
                }
            }
        }

        #endregion
    }
}